﻿/// <summary>
/// FileName: ConstantExpression.cs
/// Author: Jiang Xiaolong
/// Created Time: 2015.05.12
/// Version: 1.0
/// Company: Sunnytech
/// Function: 常量表达式；
///
/// Changed By:
/// Modification Time:
/// Discription:
/// </summary>
using System;
using Expression.api;

namespace Expression.impl
{
	public class ConstantExpression : ExpressionTree
	{
		public Type ExpType { get; set; }

		private object _value = null;

		public ConstantExpression(object value, Token.TokenType type)
		{
			this._value = value;
			if (type == Token.TokenType.BOOL)
			{
				ExpType = typeof(bool);
			}
			else if (type == Token.TokenType.DOUBLE)
			{
				ExpType = typeof(double);
			}
			else if (type == Token.TokenType.FLOAT)
			{
				ExpType = typeof(float);
			}
			else if (type == Token.TokenType.INTEGER)
			{
				ExpType = typeof(int);
			}
			else
			{
				ExpType = typeof(string);
			}
		}

		public ConstantExpression(object value, Type type)
		{
			this._value = value;
			ExpType = type;
		}

		public object Evaluate()
		{
			return _value;
		}

		public static ConstantExpression Empty()
		{
			return new ConstantExpression(null, typeof(string));
		}

		public bool IsEmpty()
		{
			if (ExpType == typeof(string) && _value == null)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public override string ToString()
		{
			if (_value != null)
			{
				return _value.ToString();
			}
			else
			{
				return "null";
			}
		}

        public ExpressionTree Clone ()
        {
            ConstantExpression expClone = new ConstantExpression(_value, ExpType);
            return expClone;
        }
	}
}

